package com.ymatou.iapi.godmch.testcase;

import static org.junit.Assume.assumeTrue;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.adminBindAgentBean;
import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.setShareTypeBean;
import com.ymatou.iapi.godmch.service.adminBindAgentCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*运营添加代理商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_adminBindAgent{
	private static adminBindAgentBean bean;
	private static adminBindAgentCall call;
	private createIndepMchBean createBean;
	private createIndepMchCall createCall;
	mch_relationWapper relationWapper=new mch_relationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("运营添加代理商");
	}

	@Before
	public void caseUp() {
		bean=new adminBindAgentBean();
		call=new adminBindAgentCall();
		createBean=new createIndepMchBean();
		createCall=new createIndepMchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**创建独立商户代理
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws InterruptedException 
	 * @throws JSONException 
	 * 
	 */
	private Integer indepAgent(String country) throws ClientProtocolException, IOException, JSONException, InterruptedException
	{
		agent_typeWapper agentTypeWapper=new agent_typeWapper();
		createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
		createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
		//申请代理
		applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
		applyBean.setCountry(country);
		applyBean.setMchId(createCall.getmchId());
		applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
		//创建代理类型
		createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
		createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
		//查询代理类型
		Map<String,Object> map=new HashMap();
		map.put("mchId", applyBean.getMchId());
		List<agent_type> list=agentTypeWapper.selectby(map);
		
		//审核通过
		approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
		approveBean.setMchId(applyBean.getMchId());
		approveBean.setAgentId(applyCall.getAgentId());
		approveBean.setAgentType(list.get(0).getAgentType());
		GodmchManageCallService.ApproveAgentCall(approveBean);
		return approveBean.getAgentId();
	}
	private void check_verify() throws InterruptedException
	{
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
		//mch_relation
		mch_relation relation=relationWapper.selectByPrimaryKey(bean.getAgentId(), bean.getMchId()).get(0);
		Logger.verifyEquals(-2, relation.getAgentType(), "验证AgentType");
		Logger.verifyEquals(-2, relation.getAgentModel(), "验证AgentModel");
		Logger.verifyEquals(-2, relation.getState(), "验证state");
		
		Logger.verifyEquals(bean.getMobile(), relation.getApplyMobile(), "验证ApplyMobile");
		Logger.verifyEquals(bean.getWechat(), relation.getApplyWechat(), "验证ApplyWechat");
		Logger.verifyEquals(bean.getRemark(), relation.getApplyRemark(), "验证ApplyRemark");
		Logger.verifyEquals(false, relation.getSamePlatform(), "验证SamePlatform");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_001() {
		Logger.start(true,"运营添加代理商-代理是独立云版本主商户-成功");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_001_1() {
		Logger.start(true,"运营添加代理商-代理开通了多级代理-成功");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//设置代理多级代理 目前设置多级代理不影响供货商绑定
			setShareTypeBean setShareTypeBean=GodmchManageCallService.initsetShareTypeV2(createCall2.getmchId());
			setShareTypeBean.setVipChannel(true);
			GodmchManageCallService.setShareTypeV2Call(setShareTypeBean);

			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_002() {
		Logger.start(true,"运营添加代理商-代理是公共云版本海外渠道商-成功");
		try {
			
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			Integer userId=GodmchManageCallService.CreateSellerMch();
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),userId);
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_003() {
		Logger.start(true,"运营添加代理商-代理是公共云版本国内渠道商-成功");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
            Integer userId = GodmchManageCallService.channl_CHN().getMchId();
            
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),userId);
			call.setData(bean);
			call.callService();			
			
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_004() {
		Logger.start(true,"运营添加代理商-代理是公共云版本国内纯代理-失败");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
            Integer userId = GodmchManageCallService.channl_CHN().getMchId();
			//国内代理
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId, userId_agent);
			createmchList.get(0).setCountry("CHN");
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			editmchBean editmchbean2 = GodmchManageCallService.IniteditmachBean(userId_agent);
			GodmchManageCallService.editmachCall(editmchbean2);
            
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),userId_agent);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商必须是独立云商户主账号或者是公有云渠道商身份"), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_005() {
		Logger.start(true,"运营添加代理商-代理是独立云版本海外供货商-失败");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierBean.setCountry("USA");
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(), applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商必须是独立云商户主账号或者是公有云渠道商身份"), "验证Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_adminBindAgent_006() {
		Logger.start(true,"运营添加代理商-代理是独立云版本国内供货商-失败(目前供货商不支持中国大陆身份注册)");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierBean.setCountry("CHN");
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(), applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商必须是独立云商户主账号或者是公有云渠道商身份"), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_007() {
		Logger.start(true,"运营添加代理商-代理是独立云版本海外代理商-失败");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			Integer agentId=this.indepAgent("USA");
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),agentId);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商必须是独立云商户主账号或者是公有云渠道商身份"), "验证Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_008() {
		Logger.start(true,"运营添加代理商-代理是独立云版本国内代理商-失败");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			Integer agentId=this.indepAgent("CHN");
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),agentId);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商必须是独立云商户主账号或者是公有云渠道商身份"), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_adminBindAgent_009() {
		Logger.start(true,"运营添加代理商-代理是独立云版本主商户-重复添加-失败");
		try {
			//商户id
			createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			
			bean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			call.setData(bean);
			call.callService();	
			call.callService();	
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("禁止重复绑定"), "验证Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}